import { getHonoContext } from "waku/unstable_hono";
import { Button } from "@/components/ui/button";
import { Link } from "waku";

import { getContext, getContextData } from "waku/middleware/context";
import * as WServer from "waku/server";
import type { PageProps } from "waku/router";
import db from "@/lib/db";
import { useCookies } from "@/lib/server/cookiebridge";
import Deck from "@/components/Flashcard/Deck";
import Deck2 from "@/components/Flashcard/Deck2";
import { CardFront, CardBack } from "@/components/Flashcard/ServerCard";

const flags: Record<string, string> = {
  th: "🇹🇭",
  en: "🇬🇧",
  zh: "🇨🇳",
  ja: "🇯🇵",
  es: "🇪🇸",
  fr: "🇫🇷",
};

export default async function HomePage(props: PageProps<"/lesson/[slug]">) {
  // const hctx: any = getHonoContext();
  // console.log({ hctx });
  // const ctx = getContext();
  // console.log(ctx.req.headers, "heders");
  // hctx.set("lol", "lmao");
  // const cokis = useCookies();
  // const coki = cokis.getCookie("sorlang");
  // console.log({ coki });
  // console.log({ props });
  // // const { user } = getContextData() as any;
  // // console.log({ user });
  const user = { id: 2 };
  const data = await getData(Number(props.slug), user.id);
  if ("error" in data) return <p>Error</p>;
  const cardComponents = data.ok.cards.map((card) => ({
    front: <CardFront data={card} />,
    back: <CardBack data={card} />,
  }));

  return (
    <>
      <section>
        <h2 className="text-lg">Thai!</h2>
        <Deck2 data={data.ok} cards={cardComponents} />
      </section>
    </>
  );
}

const getData = async (lessonId: number, userId: number) => {
  const lessons = db.fetchLesson({ userId, lessonId, random: true });
  // const lessons = db.fetchLesson({ userId, lessonId, random: false, page: 3 });
  console.log({ lessons });

  return lessons;
};

export const getConfig = async () => {
  return {
    render: "dynamic",
  } as const;
};

async function LanguageItem({ lang }: { lang: string }) {
  return (
    <div className="flex">
      <div className="text-lg">{flags[lang] || ""}</div>
    </div>
  );
}
